Android 文件上传下载

最近有个需求是图片的上传下载,因为是小功能点,所以不打算用框架写了,就httpurlconnnection+threadpool请求一下就完事了,之前调了一阵,结果下载的图片大小都是0k,还没找到原因,这里备份一下最后的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.example.testwebview.http;

import com.example.testwebview.moudle.DebugLog;
import com.example.testwebview.moudle.Environment;
import com.example.testwebview.moudle.common.Const;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpPostUploadUtil {

public static String upload() {
try {
String path = Environment.getInstance().getExternalStorageDirectory() + "/" + Const.FILE_PATH_NAME;
DebugLog.v("path", "path:" + path);
File file = new File(path);
String ur = "http://192.168.253.1:8088/groovy/ocr.groovy";
URL url = new URL(ur);

HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-quickviewplus, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setDoOutput(true);
uc.setUseCaches(true);
int size = (int) file.length();
byte[] data = new byte[size];
FileInputStream fis = new FileInputStream(file);
OutputStream out = uc.getOutputStream();
fis.read(data, 0, size);

out.write(data);
out.flush();
out.close();
fis.close();

int code = uc.getResponseCode();
String sCurrentLine = "";
String sTotalString = "";
if (code == 200) {
java.io.InputStream is = uc.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((sCurrentLine = reader.readLine()) != null)
if (sCurrentLine.length() > 0)
sTotalString = sTotalString + sCurrentLine.trim();
} else {
sTotalString = "错误:" + code;
}
return sTotalString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 * 1000); //设置超时间为3秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //防止屏蔽程序抓取而返回403错误
InputStream inputStream = conn.getInputStream(); //得到输入流
byte[] getData = readInputStream(inputStream); //获取自己数组

File saveDir = new File(savePath); //文件保存位置
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
System.out.println("info:" + url + " download success");
}

public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}